
package com.stenio.fancat.service.user;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.domain.user.UserPraise;
import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.form.user.UserPraiseForm;
import com.stenio.fancat.mapper.baby.BabyAccountMapper;
import com.stenio.fancat.mapper.baby.BabyRelationshipMapper;
import com.stenio.fancat.mapper.user.UserPraiseMapper;
import com.stenio.fancat.mapper.user.UserRelationshipMapper;
import com.stenio.fancat.mapper.user.UserRelationshipNameMapper;
import com.stenio.fancat.util.RedisUtil;
import com.stenio.fancat.vo.UserPraiseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * user点赞类
 */
@Service
public class UserPraiseService extends ServiceImpl<UserPraiseMapper, UserPraise> {

    @Autowired
    private UserPraiseMapper userPraiseMapper;

    @Autowired
    private UserRelationshipMapper userRelationshipMapper;

    @Autowired
    private UserRelationshipNameMapper userRelationshipNameMapper;

    @Autowired
    private BabyAccountMapper babyAccountMapper;

    @Autowired
    private BabyRelationshipMapper babyRelationshipMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    //亲人圈的key
    @Value("${ownBehaviorList}")
    private String key;
    //伙伴圈的key
    @Value("${partnerList}")
    private String partnerListKey;
    //伙伴动态详情的key
    @Value("${partnerBehaviorList}")
    private String partnerBehaviorList;


    /**
     * 通用查询
     * 根据userId查询babyId  (走的是缓存 --- Redis)
     * @param userId
     * @return
     */
    public String selectBabyId(String userId){
        return babyRelationshipMapper.selectBabyByUid(userId).getBabyId();
    }

    /**
     * 用户点赞的方法
     * @param userPraiseForm
     * @return
     * @throws FanCatException
     */
    @Transactional
    public UserPraiseVO insertPraise(UserPraiseForm userPraiseForm) throws FanCatException {
        if(null == userPraiseForm.getOperationId() || null == userPraiseForm.getUserId() ){
            throw new FanCatException("参数异常");
        }

        String babyId = this.selectBabyId(userPraiseForm.getUserId());

        //查询该用户是否对此任务点过赞
        //MP  拼接sql语句
        UserPraise userPraise = new UserPraise();
        Wrapper<UserPraise> eWrapper =
                new EntityWrapper<>(userPraise).eq("operation_id",userPraiseForm.getOperationId()).eq("user_id",userPraiseForm.getUserId());

        if(null != selectOne(eWrapper)){
            throw new FanCatException("0");  //0代表已经点过赞了
        }
        //插入数据  (点赞的操作)
        Integer m = userPraiseMapper.insertPraise(userPraiseForm.getOperationId(),userPraiseForm.getUserId());

        if(m <= 0){
            throw new FanCatException("插入失败，请检查参数");
        }
        //返回前端的数据
        UserPraiseVO userPraiseVO = new UserPraiseVO();
        String name = userRelationshipMapper.selectUserInfoById(userPraiseForm.getUserId()).getName();
        userPraiseVO.setUserName(name);
        int relationship = userRelationshipMapper.selectUserInfoById(userPraiseForm.getUserId()).getRelationship();
        String relationName = userRelationshipNameMapper.selectName(relationship).getRelationshipName();
        userPraiseVO.setRelationshipName(relationName);
        String babyIdSec = userRelationshipMapper.selectUserInfoById(userPraiseForm.getUserId()).getBabyId();
        String babyName = babyAccountMapper.selectByBid(babyIdSec).getNickName();
        userPraiseVO.setBabyName(babyName);

        //操作Redis
        Set<String> keys = redisTemplate.keys(babyId + "fancat" + "*");
        redisTemplate.delete(keys);

        return userPraiseVO;
    }

}
